package com.itheima.ydd.controller;

import com.itheima.ydd.common.PageResult;
import com.itheima.ydd.common.ResultData;
import com.itheima.ydd.dto.SysUserDto;
import com.itheima.ydd.entity.User;
import com.itheima.ydd.service.RoleService;
import com.itheima.ydd.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

@RestController
@RequestMapping("sys_user")
@Slf4j
public class UserController {

    @Autowired
    private UserService userService;


    /**
     * 分页条件查询 用户,和对应用户角色
     *
     * @param page  页码
     * @param limit 页面条目数
     * @param user  查询条件,用户名,用户电话,状态
     * @return 分页查询对象
     */
    @GetMapping("list")
    public ResultData getList(Long page, Long limit, User user) {
        log.info("广告位分页查询条件:page={},limit={},yddAdvpos={}", page, limit, user);
        //获取查询结果
        PageResult pageResult = userService.getList(page, limit, user);
        log.info("查询结果为:{}", pageResult);
        return ResultData.ok("", pageResult);
    }

    /**
     * 删除用户
     *
     * @param id 用户id
     * @return 提示信息
     */
    @GetMapping("del")
    public ResultData delete(String id) {
        log.info("删除用户:{}", id);
        if (id == null) return ResultData.error("该用户不存在");
        userService.removeById(id);
        return ResultData.ok("", "删除成功");
    }

    /**
     * 添加用户
     * @return  提示信息
     */
    @PostMapping("add")
    public ResultData add(@RequestBody SysUserDto sysUserDto) {
        log.info("添加用户:{}", sysUserDto);
        log.info(sysUserDto.getRole_id().toString());
        User user=new User();
        BeanUtils.copyProperties(sysUserDto,user,"role_id");
        String substring = sysUserDto.getRole_id().toString().substring(1, sysUserDto.getRole_id().toString().length()-1).replace(" ","");
        user.setRole_id("[\""+substring+"\"]");
        userService.save(user);
        return ResultData.ok("", "添加成功");
    }

    /**
     * 根据id获取对应的用户详细
     * @param id    用户id
     * @return      对应的用户数据
     */
    @GetMapping("rowInfo")
    public ResultData rowInfo(String id){
        log.info("要修改的用户id为:{}",id);
        if(id==null) return ResultData.error("该数据不存在");
        User user = userService.getById(id);
        String[] role_id = user.getRole_id().substring(2,user.getRole_id().length()-2).split(",");
        SysUserDto dto=new SysUserDto();
        BeanUtils.copyProperties(user,dto,"role_id");
        dto.setRole_id(Arrays.asList(role_id));
        log.info("查询到的数据为:{}",dto);
        return ResultData.ok("",dto);
    }


    /**
     * 管理员修改
     * @return  提示信息
     */
    @PostMapping("update")
    public ResultData update(@RequestBody SysUserDto sysUserDto){
        User user=new User();
        BeanUtils.copyProperties(sysUserDto,user,"role_id");
        if(sysUserDto.getRole_id()!=null) {
            String substring = sysUserDto.getRole_id().toString().substring(1, sysUserDto.getRole_id().toString().length() - 1).replace(" ", "");
            user.setRole_id("[\"" + substring + "\"]");
        }
        log.info("修改用户数据:{}",user);
        userService.updateById(user);
        return ResultData.ok("","修改成功");
    }


}
